package com.atguigu.dao.impl;

import com.atguigu.dao.RedisServiceConfigDao;
import com.atguigu.pojo.RedisServiceConfig;

import java.util.List;

public class RedisServiceConfigDaoImpl extends BaseDao implements RedisServiceConfigDao {
    @Override
    public int addRedisServiceConfig(RedisServiceConfig redisServiceConfig) {
        System.out.println("RedisServiceConfigDaoImpl 程序在["+Thread.currentThread().getName()+"]线程中");
        String sql="insert into t_redisServiceConfig(`redisServiceConfigId`,`serverId`,`serviceId`,`port`,`password`,`maxMemory`,`masterNode`) values(?,?,?,?,?,?,?);";
            return update(sql,redisServiceConfig.getRedisServiceConfigId(),redisServiceConfig.getServerId(),
                    redisServiceConfig.getServiceId(),redisServiceConfig.getPort(),
                    redisServiceConfig.getPassword(),redisServiceConfig.getMaxMemory(),redisServiceConfig.getMasterNode());
    }

    @Override
    public int deleteRedisServiceConfigById(String redisServiceConfigId) {
        String sql="delete from t_redisServiceConfig where redisServiceConfigId=?";
        return update(sql,redisServiceConfigId);
    }

    @Override
    public int updateRedisServiceConfig(RedisServiceConfig redisServiceConfig) {
        String sql="update t_redisServiceConfig set `serverId`=?,`serviceId`=?,`port`=?,`password`=?,`maxMemory`=?,`masterNode`=? where redisServiceConfigId=?";
        return update(sql,redisServiceConfig.getServerId(),
                redisServiceConfig.getServiceId(),redisServiceConfig.getPort(),
                redisServiceConfig.getPassword(),redisServiceConfig.getMaxMemory(),
                redisServiceConfig.getMasterNode(),redisServiceConfig.getRedisServiceConfigId());
    }

    @Override
    public RedisServiceConfig queryRedisServiceConfigById(String redisServiceConfigId) {
        String sql ="select `redisServiceConfigId`,`serverId`,`serviceId`,`port`,`password`,`maxMemory`,`masterNode` from t_redisServiceConfig where redisServiceConfigId=?";
        return queryForOne(RedisServiceConfig.class,sql,redisServiceConfigId);
    }

    @Override
    public List<RedisServiceConfig> queryRedisServiceConfigs() {
        String sql ="select `redisServiceConfigId`,`serverId`,`serviceId`,`port`,`password`,`maxMemory`,`masterNode`  from t_redisServiceConfig";
        return queryForList(RedisServiceConfig.class,sql);
    }
    @Override
    public RedisServiceConfig queryRedisServiceConfigByServiceId(String serviceId){
        String sql ="select `redisServiceConfigId`,`serverId`,`serviceId`,`port`,`password`,`maxMemory`,`masterNode`  from t_redisServiceConfig where serviceId =?";
        return queryForOne(RedisServiceConfig.class,sql,serviceId);
    }
    @Override
    public List<RedisServiceConfig> queryRedisServiceConfigsByPort(String port) {
        String sql ="select `redisServiceConfigId`,`serverId`,`serviceId`,`port`,`password`,`maxMemory`,`masterNode` from t_redisServiceConfig where port =?";
        return queryForList(RedisServiceConfig.class,sql,port);
    }

    @Override
    public Integer queryForPageTotalCount() {
        String sql="select count(*) from t_redisServiceConfig";
        Number serverCount=(Number) queryForSingleValue(sql);//这个语句似乎可以返回任何类型的查询
        //System.out.println(bookCount);
        return serverCount.intValue();
    }

    @Override
    public List<RedisServiceConfig> queryForPageItems(Integer begin, Integer pageSize) {
        String sql ="select `redisServiceConfigId`,`serverId`,`serviceId`,`port`,`password`,`maxMemory`,`masterNode` from t_redisServiceConfig limit ?,?";
        return queryForList(RedisServiceConfig.class,sql,begin,pageSize);

    }
}
